<template>
  <div>
    <!--质量信息反馈达标率 -->
    <TopFilter ref="TopFilterss" :list="topFilterList" :cols="3" :collapse="collapse" labelWidth="120" @filterChange="changeHandle" @resetChange="resetHandle"></TopFilter>
    <FilterTable
      ref="table"
      :columns="columns"
      :params="fetchParams"
      :fetchapi="fetchapi"
      :datakey="'records'"
      :onColumnsChange="(columns) => (this.columns = columns)"
      :is-select-column="false"
      columnsRef="qualityControlRates"
      :isColumnFilter="false"
    >
      <template slot="controls" slot-scope="props">
        <multiuse-button size="small" type="primary" :click="exportFileBtn.bind()">导出</multiuse-button>
      </template>
    </FilterTable>

    <!--  服务中心选择 -->
    <BaseDialog :visible.sync="selectDelearFlag" title="服务中心选择" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <DealerList @close="Delearclose" @save="Delearsave" selectionType="multiple" />
    </BaseDialog>
  </div>
</template>
<script>
import { exportExcelFile, notifyAction } from '@/utils/index';
import { geQualityControlRates, getQualityExport } from '@/api/serviceass/qualityControlRates';
import { dictionary } from '@/utils/dictMixin';
import { getRegion, getLevelOneArea, getLevelTwoAndThreeArea } from '@/api/cms-common/dealerdata/index'; //事业部，办事处
import DealerList from '@/pages/claimDemage/tools/DealerList.vue';

export default {
  mixins: [dictionary],
  components: { DealerList },
  data() {
    return {
      visible: false,
      collapse: false,
      fetchapi: geQualityControlRates,
      topFilterList: this.topFilterListFun(),
      columns: this.creatHeader(),
      fetchParams: {},
      responsibleCode: null,
      selectDelearFlag: false,
      selectDealerData: [],
      dealerData: [],
      ORG_TYPE_SERVICE_WAR_AREA: 15061019, //服务战区
      ORG_TYPE_SERVICE_BIG_AREA: 15061020, //服务大区
      ORG_TYPE_SERVICE_SMALL_AREA: 15061021, //服务小区
    };
  },
  mounted() {
    this.getTopLevelArea(this.ORG_TYPE_SERVICE_WAR_AREA, 'serviceWarArea');
  },
  methods: {
    //搜索数据
    topFilterListFun() {
      return [
        {
          type: 'SELECT',
          label: '服务战区',
          fieldName: 'serviceWarArea',
          placeholder: '请选择',
          initialValue: '',
          itemList: [],
          change: (val) => {
            console.log('val',val)
            this.changeTopAndMiddleLevelArea(val, this.ORG_TYPE_SERVICE_BIG_AREA, 'serviceWarArea', 'serviceBigArea', 'serviceSmallArea');
          },
        },
        {
          type: 'SELECT',
          label: '服务大区',
          fieldName: 'serviceBigArea',
          placeholder: '请选择',
          initialValue: '',
          itemList: [],
          change: (val) => {
            this.changeTopAndMiddleLevelArea(val, this.ORG_TYPE_SERVICE_SMALL_AREA, 'serviceBigArea', 'serviceSmallArea', 'serviceSmallArea');
          },
        },
        {
          type: 'SELECT',
          label: '服务小区',
          fieldName: 'serviceSmallArea',
          placeholder: '请选择',
          initialValue: '',
          itemList: [],
        },
        {
          type: 'INPUT',
          label: '服务中心',
          render: (p) => {
            return (
              <div>
                <el-select v-model={this.selectDealerData} disabled={true} multiple placeholder="请选择">
                  {this.dealerData.map((item) => (
                    <el-option key={item.dealerCode} label={item.dealerShortname} value={item.dealerCompanyCode} />
                  ))}
                </el-select>
                <multiuse-button style="position: absolute;right: 0;height:100%" icon="el-icon-search" onClick={() => (this.selectDelearFlag = true)} />
              </div>
            );
          },
        },
        {
          type: 'INPUT',
          label: '年份',
          fieldName: 'applyYear',
          placeholder: '请输入例如:2022',
          initialValue: '',
          rules: [{ required: true, message: '请输入年份', trigger: 'blur' }],
        },
      ];
    },
    //table表头和数据源
    creatHeader() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          fixed: 'left',
          sorter: true,
          render: (props) => {
            return <span>{props.row.index + 1}</span>;
          },
        },
        { title: '服务中心代码', dataIndex: 'dealerCode', sorter: true },
        { title: '服务中心简称', dataIndex: 'dealerShortname' },
        { title: '服务战区', dataIndex: 'serviceWarAreaName' },
        { title: '服务大区', dataIndex: 'serviceBigAreaName' },
        { title: '服务小区', dataIndex: 'serviceSmallAreaName' },
        {
          title: '一月',
          align: 'center',
          children: [
            {
              title: '一次通过数',
              sorter: true,
              render: (props) => {
                let divhtml = `${props.row.month.month1.number}`;
                return <div domPropsInnerHTML={divhtml}></div>;
              },
            },
            {
              title: '总数',
              sorter: true,
              render: (props) => {
                let divhtml = `${props.row.month.month1.sum}`;
                return <div domPropsInnerHTML={divhtml}></div>;
              },
            },
            {
              title: '一次通过率 (%)',
              sorter: true,
              render: (props) => {
                let divhtml = `${props.row.month.month1.passingRate.toFixed(2)}`;
                return <div domPropsInnerHTML={divhtml}></div>;
              },
            },
          ],
        },
        {
          title: '二月',
          align: 'center',
          children: [
            {
              title: '一次通过数',
              sorter: true,
              render: (props) => {
                let divhtml = `${props.row.month.month2.number}`;
                return <div domPropsInnerHTML={divhtml}></div>;
              },
            },
            {
              title: '总数',
              sorter: true,
              render: (props) => {
                let divhtml = `${props.row.month.month2.sum}`;
                return <div domPropsInnerHTML={divhtml}></div>;
              },
            },
            {
              title: '一次通过率 (%)',
              sorter: true,
              render: (props) => {
                let divhtml = `${props.row.month.month2.passingRate.toFixed(2)}`;
                return <div domPropsInnerHTML={divhtml}></div>;
              },
            },
          ],
        },
        {
          title: '三月',
          align: 'center',
          children: [
            {
              title: '一次通过数',
              sorter: true,
              render: (props) => {
                let divhtml = `${props.row.month.month3.number}`;
                return <div domPropsInnerHTML={divhtml}></div>;
              },
            },
            {
              title: '总数',
              sorter: true,
              render: (props) => {
                let divhtml = `${props.row.month.month3.sum}`;
                return <div domPropsInnerHTML={divhtml}></div>;
              },
            },
            {
              title: '一次通过率 (%)',
              sorter: true,
              render: (props) => {
                let divhtml = `${props.row.month.month3.passingRate.toFixed(2)}`;
                return <div domPropsInnerHTML={divhtml}></div>;
              },
            },
          ],
        },
        {
          title: '四月',
          align: 'center',
          children: [
            {
              title: '一次通过数',
              sorter: true,
              render: (props) => {
                let divhtml = `${props.row.month.month4.number}`;
                return <div domPropsInnerHTML={divhtml}></div>;
              },
            },
            {
              title: '总数',
              sorter: true,
              render: (props) => {
                let divhtml = `${props.row.month.month4.sum}`;
                return <div domPropsInnerHTML={divhtml}></div>;
              },
            },
            {
              title: '一次通过率 (%)',
              sorter: true,
              render: (props) => {
                let divhtml = `${props.row.month.month4.passingRate.toFixed(2)}`;
                return <div domPropsInnerHTML={divhtml}></div>;
              },
            },
          ],
        },
        {
          title: '五月',
          align: 'center',
          children: [
            {
              title: '一次通过数',
              sorter: true,
              render: (props) => {
                let divhtml = `${props.row.month.month5.number}`;
                return <div domPropsInnerHTML={divhtml}></div>;
              },
            },
            {
              title: '总数',
              sorter: true,
              render: (props) => {
                let divhtml = `${props.row.month.month5.sum}`;
                return <div domPropsInnerHTML={divhtml}></div>;
              },
            },
            {
              title: '一次通过率 (%)',
              sorter: true,
              render: (props) => {
                let divhtml = `${props.row.month.month5.passingRate.toFixed(2)}`;
                return <div domPropsInnerHTML={divhtml}></div>;
              },
            },
          ],
        },
        {
          title: '六月',
          align: 'center',
          children: [
            {
              title: '一次通过数',
              sorter: true,
              render: (props) => {
                let divhtml = `${props.row.month.month6.number}`;
                return <div domPropsInnerHTML={divhtml}></div>;
              },
            },
            {
              title: '总数',
              sorter: true,
              render: (props) => {
                let divhtml = `${props.row.month.month6.sum}`;
                return <div domPropsInnerHTML={divhtml}></div>;
              },
            },
            {
              title: '一次通过率 (%)',
              sorter: true,
              render: (props) => {
                let divhtml = `${props.row.month.month6.passingRate.toFixed(2)}`;
                return <div domPropsInnerHTML={divhtml}></div>;
              },
            },
          ],
        },
        {
          title: '七月',
          align: 'center',
          children: [
            {
              title: '一次通过数',
              sorter: true,
              render: (props) => {
                let divhtml = `${props.row.month.month7.number}`;
                return <div domPropsInnerHTML={divhtml}></div>;
              },
            },
            {
              title: '总数',
              sorter: true,
              render: (props) => {
                let divhtml = `${props.row.month.month7.sum}`;
                return <div domPropsInnerHTML={divhtml}></div>;
              },
            },
            {
              title: '一次通过率 (%)',
              sorter: true,
              render: (props) => {
                let divhtml = `${props.row.month.month7.passingRate.toFixed(2)}`;
                return <div domPropsInnerHTML={divhtml}></div>;
              },
            },
          ],
        },
        {
          title: '八月',
          align: 'center',
          children: [
            {
              title: '一次通过数',
              sorter: true,
              render: (props) => {
                let divhtml = `${props.row.month.month8.number}`;
                return <div domPropsInnerHTML={divhtml}></div>;
              },
            },
            {
              title: '总数',
              sorter: true,
              render: (props) => {
                let divhtml = `${props.row.month.month8.sum}`;
                return <div domPropsInnerHTML={divhtml}></div>;
              },
            },
            {
              title: '一次通过率 (%)',
              sorter: true,
              render: (props) => {
                let divhtml = `${props.row.month.month8.passingRate.toFixed(2)}`;
                return <div domPropsInnerHTML={divhtml}></div>;
              },
            },
          ],
        },
        {
          title: '九月',
          align: 'center',
          children: [
            {
              title: '一次通过数',
              sorter: true,
              render: (props) => {
                let divhtml = `${props.row.month.month9.number}`;
                return <div domPropsInnerHTML={divhtml}></div>;
              },
            },
            {
              title: '总数',
              sorter: true,
              render: (props) => {
                let divhtml = `${props.row.month.month9.sum}`;
                return <div domPropsInnerHTML={divhtml}></div>;
              },
            },
            {
              title: '一次通过率 (%)',
              sorter: true,
              render: (props) => {
                let divhtml = `${props.row.month.month9.passingRate.toFixed(2)}`;
                return <div domPropsInnerHTML={divhtml}></div>;
              },
            },
          ],
        },
        {
          title: '十月',
          align: 'center',
          children: [
            {
              title: '一次通过数',
              sorter: true,
              render: (props) => {
                let divhtml = `${props.row.month.month10.number}`;
                return <div domPropsInnerHTML={divhtml}></div>;
              },
            },
            {
              title: '总数',
              sorter: true,
              render: (props) => {
                let divhtml = `${props.row.month.month10.sum}`;
                return <div domPropsInnerHTML={divhtml}></div>;
              },
            },
            {
              title: '一次通过率 (%)',
              sorter: true,
              render: (props) => {
                let divhtml = `${props.row.month.month10.passingRate.toFixed(2)}`;
                return <div domPropsInnerHTML={divhtml}></div>;
              },
            },
          ],
        },
        {
          title: '十一月',
          align: 'center',
          children: [
            {
              title: '一次通过数',
              sorter: true,
              render: (props) => {
                let divhtml = `${props.row.month.month11.number}`;
                return <div domPropsInnerHTML={divhtml}></div>;
              },
            },
            {
              title: '总数',
              sorter: true,
              render: (props) => {
                let divhtml = `${props.row.month.month11.sum}`;
                return <div domPropsInnerHTML={divhtml}></div>;
              },
            },
            {
              title: '一次通过率 (%)',
              sorter: true,
              render: (props) => {
                let divhtml = `${props.row.month.month11.passingRate.toFixed(2)}`;
                return <div domPropsInnerHTML={divhtml}></div>;
              },
            },
          ],
        },
        {
          title: '十二月',
          align: 'center',
          children: [
            {
              title: '一次通过数',
              sorter: true,
              render: (props) => {
                let divhtml = `${props.row.month.month12.number}`;
                return <div domPropsInnerHTML={divhtml}></div>;
              },
            },
            {
              title: '总数',
              sorter: true,
              render: (props) => {
                let divhtml = `${props.row.month.month12.sum}`;
                return <div domPropsInnerHTML={divhtml}></div>;
              },
            },
            {
              title: '一次通过率 (%)',
              sorter: true,
              render: (props) => {
                let divhtml = `${props.row.month.month12.passingRate.toFixed(2)}`;
                return <div domPropsInnerHTML={divhtml}></div>;
              },
            },
          ],
        },
        {
          title: '一季度',
          children: [
            {
              title: '一次通过率 (%)',
              sorter: true,
              render: (props) => {
                let divhtml = `${props.row.quarter.quarter0 == null ? '0.00' : props.row.quarter.quarter0.toFixed(2)}`;
                return <div domPropsInnerHTML={divhtml}></div>;
              },
            },
          ],
        },
        {
          title: '二季度',
          children: [
            {
              title: '一次通过率 (%)',
              sorter: true,
              render: (props) => {
                let divhtml = `${props.row.quarter.quarter1 == null ? '0.00' : props.row.quarter.quarter1.toFixed(2)}`;
                return <div domPropsInnerHTML={divhtml}></div>;
              },
            },
          ],
        },
        {
          title: '三季度',
          children: [
            {
              title: '一次通过率 (%)',
              sorter: true,
              render: (props) => {
                let divhtml = `${props.row.quarter.quarter2 == null ? '0.00' : props.row.quarter.quarter2.toFixed(2)}`;
                return <div domPropsInnerHTML={divhtml}></div>;
              },
            },
          ],
        },
        {
          title: '四季度',
          children: [
            {
              title: '一次通过率 (%)',
              sorter: true,
              render: (props) => {
                let divhtml = `${props.row.quarter.quarter3 == null ? '0.00' : props.row.quarter.quarter3.toFixed(2)}`;
                return <div domPropsInnerHTML={divhtml}></div>;
              },
            },
          ],
        },
      ];
    },
    changeHandle(val) {
      if (val.responsiblePerson) {
        val.responsibleCode = this.responsibleCode;
      } else {
        val.responsibleCode = null;
      }
      if (val.applyYear == '') {
        val.applyYear = null;
      }
      this.fetchParams = Object.assign({}, this.fetchParams, val, { dealerCode: this.searchDealerCode, dealerCompanyCode: this.selectDealerData.length ? this.selectDealerData : null });
    },
    async exportFileBtn() {
      await getQualityExport(this.fetchParams).then((res) => {
        console.log(res);
        if (res.status !== 'failure') {
          exportExcelFile(res.data, '质量信息反馈达标率');
        } else {
          notifyAction('导出失败', 'error');
        }
      });
    },
    close() {
      this.visible = false;
      this.fetchParams = Object.assign({}, val);
    },
    Delearclose() {
      this.selectDelearFlag = false;
    },
    //服务中心选择保存
    Delearsave(obj) {
      console.log('选择数据传递', obj);
      this.dealerData = obj;
      this.selectDealerData = [];
      obj.forEach((item) => {
        this.selectDealerData.push(item.dealerCompanyCode);
      });
      this.selectDelearFlag = false;
    },
    resetHandle(val) {
      val.applyYear = null;
      this.dealerData = [];
      this.selectDealerData = [];
      this.fetchParams = Object.assign({}, this.fetchParams, val, { dealerCompanyCode: null }, { xhrAbort: false });
    },
    //获取大区
    async getBigArea() {
      const res = await getRegion();
      if (res.resultCode === 200) {
        this.topFilterList.find((item) => item.fieldName === 'serviceBigArea').itemList = res.data.map((x) => ({ value: parseInt(x.id), text: x.orgName }));
      }
    },
    //获取顶层组织数据
    async getTopLevelArea(areaType, filedName) {
      const res = await getLevelOneArea(areaType);
      if (res.resultCode === 200) {
        this.topFilterList.find((item) => item.fieldName === filedName).itemList = res.data.map((x) => ({ value: parseInt(x.id), text: x.orgName }));
      }
    },
    //顶层和中层组织数据修改
    async changeTopAndMiddleLevelArea(deptId, sonAreaType, parentFieldName, sonFieldName, smallFieldName, flag = true) {
      let sonArea = this.topFilterList.find((item) => item.fieldName === sonFieldName);
      let smallArea = this.topFilterList.find((item) => item.fieldName === smallFieldName);
      if (!deptId) {
        sonArea.initialValue = '';
        sonArea.itemList = [];
        smallArea.initialValue = '';
        smallArea.itemList = [];
      } else {
        if (flag) {
          //正常的级联，清空原来选择的数据
          sonArea.initialValue = '';
          smallArea.initialValue = '';
        }
        const res = await getLevelTwoAndThreeArea(sonAreaType, deptId);
        if (res.resultCode === 200) {
          sonArea.itemList = res.data.map((x) => ({ value: parseInt(x.id), text: x.orgName }));
        }
      }
    },
  },
};
</script>
<style lang="less" scoped>
</style>

